
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import sun.plugin2.message.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class Massage{
    public String from;
    public String to;
    public String words;
}

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        ArrayList<Massage> arrayList;
        try {
            arrayList=set();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.getWriter().write(objectMapper.writeValueAsString(arrayList));
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Massage massage=objectMapper.readValue(req.getInputStream(),Massage.class);

        try {
            save(massage);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        resp.setStatus(200);
        System.out.println("已接收到请求："+massage.from+"对"+massage.to+"说"+massage.words);
    }
    public void save(Massage massage) throws SQLException {

        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/表白墙?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("666666");

        Connection connection=dataSource.getConnection();

        String sql="insert into massage values (?,?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,massage.from);
        statement.setString(2,massage.to);
        statement.setString(3,massage.words);

         int ret=statement.executeUpdate();
        System.out.println("操作了"+ret+"行");

        statement.close();
        connection.close();
    }
    public ArrayList<Massage> set() throws SQLException {

        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/表白墙?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("666666");

        Connection connection=dataSource.getConnection();

        String sql="select * from massage";
        PreparedStatement statement=connection.prepareStatement(sql);

        ResultSet resultSet =statement.executeQuery();
        ArrayList<Massage> arrayList=new ArrayList<>();
        while (resultSet.next()) {
            Massage massage = new Massage();
            massage.from = resultSet.getString("from");
            massage.to = resultSet.getString("to");
            massage.words = resultSet.getString("words");
            arrayList.add(massage);
        }

        resultSet.close();
        statement.close();
        connection.close();
        return arrayList;
    }
}
